package CalendarComponents;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import Managers.Category;

/**
 * Class used to provide the JComboBox with the ability to display JLabels, here used for visualising categories
 * @author Christian G.
 */
@SuppressWarnings("serial")
public class CategoryRenderer extends JLabel implements ListCellRenderer{
	
	public CategoryRenderer(){
		this.setOpaque(true);
		this.setHorizontalAlignment(LEFT);
		this.setVerticalAlignment(CENTER);
	}
	
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
		
	    //Get the selected index. (The index param isn't
        //always valid, so just use the value.)
        String description = ((Category)value).getName();
        Color color = ((Category)value).getColor();

        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }

        //Set the icon and text.  If icon was null, say so.
        /*
        BufferedImage img = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
        img.getGraphics().setColor(Color.RED);
        img.getGraphics().fillRect(0, 0, 10, 10);
         */
        
        ColorIcon icon = new ColorIcon(10,10,color);
        setIcon(icon);

        setText(description);
        
		return this;
		
	}
}
